#include "analy/logic/ToolLogicBT.h"

ToolLogicBT::ToolLogicBT(QMap<QString, QMap<QString, QString>>  &_RGLRLSMAP_,
                         QSqlDatabase *DB,
                         QMap<QString, QString> &_QSSMAP_)
    :ToolLogicCT(_RGLRLSMAP_, DB, _QSSMAP_){

    diary("init successful", Diary::Debug)

}


void ToolLogicBT::data_prograss_step(int step){
    if (!DT_anlyGbar->isVisible()) DT_anlyGbar->setVisible(true);
    DT_anlyGbar->setValue(step);
}


void ToolLogicBT::data_execbut_stat(bool stat){
    if (stat)
    {
        DT_exec->doconfigur_text("执行");
        DT_scdtEdit->setReadOnly(false);
        DT_scdtEdit->setCenterOnScroll(true);
        DT_scdtEdit->document()->setModified(true);
        DT_scdtEdit->document()->setUndoRedoEnabled(true);
        DT_scdtEdit->document()->setUseDesignMetrics(true);
        if (DT_anlyGbar->isVisible()) {DT_anlyGbar->setVisible(false); DT_anlyGbar->setValue(0);};
    }
    else
    {
        DT_exec->doconfigur_text("执行中");
        DT_scdtEdit->setReadOnly(true);
        DT_scdtEdit->setCenterOnScroll(false);
        DT_scdtEdit->document()->setModified(false);
        DT_scdtEdit->document()->setUndoRedoEnabled(false);
        DT_scdtEdit->document()->setUseDesignMetrics(false);
    }
}


void ToolLogicBT::data_execbut_clickev(){
    if (QList<int>({NT::CUT_FROMCNEDIT, NT::CUT_FROMODEDIT}).contains(impact_ttype[RPLKEY.CUT_EXBUTCHANGE]))
    {
        BS_listWidget->currentItem()->setFlags(BS_listWidget->currentItem()->flags());
        QString UpdateSql;
        QString NikeName = impact_str[RPLKEY.UPD_CNLIST_ITEMS].split("&&").last();
        if (impact_ttype[RPLKEY.CUT_EXBUTCHANGE] == NT::CUT_FROMODEDIT)
        {
            impact_ttype[RPLKEY.CUT_EXBUTCHANGE] = NT::CUT_COMMANDLIST;
            QStringList newNikeNameList;
            for(int row = 0; row < BS_listWidget->count(); row++) newNikeNameList.append(BS_listWidget->item(row)->text());
            UpdateSql = QString("UPDATE serial SET ordernike='%1' WHERE sp_name='%2'").arg(
                        newNikeNameList.join("&&"), NikeName);
            DT_exec->doconfigur_text("发送");
        }
        else if (impact_ttype[RPLKEY.CUT_EXBUTCHANGE] == NT::CUT_FROMCNEDIT)
        {
            impact_ttype[RPLKEY.CUT_EXBUTCHANGE] = NT::CUT_CONNECTLIST;
            UpdateSql = QString("UPDATE serial SET sp_name='%1' WHERE sp_name='%2'").arg(
                        BS_listWidget->currentItem()->text(), NikeName);
            DT_exec->doconfigur_text("应用");
        }

        if (Query->exec(UpdateSql))
        {
            data_serial_Infoquery();
            diary("edition successful and submit finish", Diary::Debug)
        }
        BS_listWidget->setStyleSheet((*__QSSMAP__)["anl_lstwin"].arg(13));
    }
    else
    {
        if (impact_ttype[RPLKEY.CUT_EXBUTCHANGE] == NT::CUT_FROMQREDIT)
        {
            int currentRow = impact_stint[RPLKEY.UPD_QRLIST_ROWS].last();
            QR_aliasEdit->setPlainText(BS_listWidget->item(currentRow)->text());
            //# 代码执行顺序调整，保持 CUT_FROMQREDIT 枚举，可让 data_qrcodebranch 作为区别于别名文本框输入和列表编辑的依据
            data_qrcodebranch(NT::CUT_QRCODESAVE, true); //#保存并更新
            impact_ttype[RPLKEY.CUT_EXBUTCHANGE] = NT::CUT_QRCODELIST;
            DT_exec->doconfigur_text("生成");
            BS_listWidget->setStyleSheet((*__QSSMAP__)["AnalyCT_ListWidget"].arg(13));
        }

        else if (impact_ttype[RPLKEY.CUT_EXBUTCHANGE] == NT::CUT_COMMANDLIST)
        {
            data_serial_commandbranch(NT::CUT_COMMANDSEND);
            diary("command code exec", Diary::Debug)
        }

        else if (impact_ttype[RPLKEY.CUT_EXBUTCHANGE] == NT::CUT_CONNECTLIST)
        {
            if(!impact_str["inAdvanceCNitem"].isEmpty()) data_serial_summaryconfig(true, impact_str["inAdvanceCNitem"]);
            data_serial_connectbranch(NT::CUT_SERIALCONNECT);
            diary("connect start", Diary::Debug)
        }

        else if (impact_ttype[RPLKEY.CUT_EXBUTCHANGE] == NT::CUT_QRCODELIST)
        {
            data_qrcodebranch(NT::CUT_QRCODEPROD);
            diary("qrcode product", Diary::Debug)
        }

        else if (impact_ttype[RPLKEY.CUT_EXBUTCHANGE] == NT::CUT_ANALYLIST)
        {
            if (BS_listWidget->currentItem())
            {
                if (BS_listWidget->currentItem()->text() == "Unicode/Chz")
                {
                    QString rstext;
                    PPScriptExec::PyScript_UnitTchz("outer_script", "unic_utf_res", DT_scdtEdit->toPlainText().toStdString().c_str(), rstext);
                    DT_rsdtEdit->setPlainText(rstext);
                    data_txtboxasb_facecut(DT_rsview->objectName(), false);
                    PBL_centraStackWidget->setCurrentWidget(DT_rsdtEdit);
                }
            }
            diary("analy exec", Diary::Debug)
        }

        else if (impact_ttype[RPLKEY.CUT_EXBUTCHANGE] == NT::CUT_HLITEXTLIST)
        {
            diary("highlight click", Diary::Debug)
        }
    }
}
